package com.btex.app.treaty.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.entity.YuCheLeverInfo;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.btex.app.tools.Tools;
import com.btex.app.tools.Util;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/15
 * @time 下午6:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class CangweiMoshiDialog extends Dialog {


    @NonNull
    private Context mContext;
    private ArrayList<String> mStrings;
    private String mLeverage;
    private String mGanggan;
    private String mMIs_cross;
    private String mAllow_cross;
    private String mPriceNumber;
    private String mPrice_float_num = "4";
    @BindView(R.id.zhucang)
    Button mZhucang;
    @BindView(R.id.quancang)
    Button mQuancang;
    @BindView(R.id.jiazai)
    ImageView mJiazai;
    @BindView(R.id.dialog_traety_earnest_tv_sure_lin)
    LinearLayout mDialogTraetyEarnestTvSureLin;
    @BindView(R.id.baozhengjin)
    TextView mBaozhengjin;
    @BindView(R.id.qiangpingjia)
    TextView mQiangpingjia;
    @BindView(R.id.dialog_traety_earnest_tv_sure)
    TextView mDialogTraetyEarnestTvSure;

    public String isQuan = "1";

    public CangweiMoshiDialog(@NonNull Context context, ArrayList<String> strings, String leverage, String ganggan, String mIs_cross, String allow_cross, String priceNumber, String Price_float_num) {
        super(context, R.style.coupon_style);

        mContext = context;
        mStrings = strings;
        mLeverage = leverage;
        mGanggan = ganggan;
        mMIs_cross = mIs_cross;
        mAllow_cross = allow_cross;
        mPriceNumber = priceNumber;
        mPrice_float_num = Price_float_num;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cangwei_moshi_dialog);
        ButterKnife.bind(this);
        Glide.with(getContext()).load(R.drawable.jiazai).into(mJiazai);
        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.9);
        attributes.height = (int) (heightPixels * 0.45);
        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);

        if (mMIs_cross.equals("1")) {
            mQuancang.setSelected(true);
        } else {
            mZhucang.setSelected(true);
        }
    }


    public void SetStat() {
        mZhucang.setSelected(false);
        mQuancang.setSelected(false);
    }

    @OnClick({R.id.zhucang, R.id.quancang, R.id.jiazai, R.id.dialog_traety_earnest_tv_sure_lin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.zhucang:
                setStatePost();
                mMIs_cross = "-1";
                SetStat();
                mZhucang.setSelected(true);
                break;
            case R.id.quancang:

                if (!TextUtils.isEmpty(mAllow_cross)) {
                    if ("1".equals(mAllow_cross)) {
                        setStatePost();
                        mMIs_cross = "1";
                        SetStat();
                        mQuancang.setSelected(true);
                    } else {
                        Util.showSnachBarDialog(mBaozhengjin, mContext.getResources().getColor(R.color.red), "不支持全仓模式");
                    }
                }

                break;
            case R.id.jiazai:
                break;
            case R.id.dialog_traety_earnest_tv_sure_lin:

                mDialogTraetyEarnestTvSureLin.setClickable(false);
                mJiazai.setVisibility(View.VISIBLE);

                getValue();

                break;
        }
    }


    public void setStatePost() {
        int num = (int) ((Math.random() * 9 + 1) * 100000);

        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
        RequestParams params = new RequestParams();
        params.addBodyParameter("position_id", mLeverage);
        params.addBodyParameter("is_cross", mMIs_cross);
        params.addBodyParameter("lang", "zh_cn");

        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.PRE_SET_CROSS_STATE + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Log.i("nchwetwekwueh", responseInfo.result);


                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);

                    if (jsonObject.getString("code").equals("1")) {
                        Gson gson = new Gson();
                        YuCheLeverInfo yuCheLeverInfo = gson.fromJson(responseInfo.result, YuCheLeverInfo.class);

                        String New_margin = Tools.round(Double.valueOf(yuCheLeverInfo.getData().getNew_margin()), Integer.valueOf(mPriceNumber)).toString().trim();
                        String New_liq_price = Tools.round(Double.valueOf(yuCheLeverInfo.getData().getNew_liq_price()), Integer.valueOf(mPrice_float_num)).toString().trim();
                        mQiangpingjia.setText(New_liq_price);
                        mBaozhengjin.setText(New_margin);

                    } else {

                        Util.showSnachBarDialog(mBaozhengjin, MyApplication.getContextObject().getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }

                } catch (JSONException e) {

                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }

        });
    }

    public void getValue() {

        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
        RequestParams params = new RequestParams();
        params.addBodyParameter("position_id", mLeverage);
        params.addBodyParameter("is_cross", mMIs_cross);
        params.addBodyParameter("lang", "zh_cn");

        int num = (int) ((Math.random() * 9 + 1) * 100000);
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.SET_CROSS_STATE + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                mDialogTraetyEarnestTvSureLin.setClickable(true);
                mJiazai.setVisibility(View.GONE);
                Log.i("nchwetwekwueh", responseInfo.result);
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);

                    if (jsonObject.getString("code").equals("1")) {

                        dismiss();
                    } else {

                        Util.showSnachBarDialog(mBaozhengjin, MyApplication.getContextObject().getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }

                } catch (JSONException e) {

                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }

        });
    }


}
